package com.kaka.bus.adapter;

import android.app.Activity;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kaka.bus.R;
import com.kaka.bus.bean.DrainProductBean;
import com.kaka.bus.view.DensityUtil;

import java.util.List;

/**
 * Created by OnionMac on 2018/7/11.
 */

public class MainProAdapter extends BaseQuickAdapter<DrainProductBean, BaseViewHolder> {
    private Activity mActivity;
    Typeface typeface;

    public MainProAdapter(Activity activity, int layoutResId, @Nullable List<DrainProductBean> data) {
        super(layoutResId, data);
        mActivity = activity;
        typeface = Typeface.createFromAsset(mActivity.getAssets(), "HELVETICANEUELTPRO-MDCN_0.OTF");

    }

    @Override
    protected void convert(BaseViewHolder helper, DrainProductBean item) {
        if (helper.getLayoutPosition() == mData.size() - 1) {
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) helper.itemView.getLayoutParams();
            layoutParams.bottomMargin = DensityUtil.dp2px(20);
            helper.itemView.setLayoutParams(layoutParams);
        } else {
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) helper.itemView.getLayoutParams();
            layoutParams.bottomMargin = 0;
            helper.itemView.setLayoutParams(layoutParams);
        }
        helper.setText(R.id.tv_title, item.getProduct().getProductName());
        helper.setText(R.id.tv_subhead, item.getProduct().getProductDescription());

        TextView textView = helper.getView(R.id.tv_money);
        textView.setTypeface(typeface);
        if (item.getProduct().getRange().contains("万")) {
            textView.setText(com.kaka.bus.util.Test.lastWordzoomOut(item.getProduct().getRange(), 10));
        } else {
            textView.setText(item.getProduct().getRange());
        }
        ImageView img_icon = helper.getView(R.id.img_icon);
        Glide.with(mActivity)
                .load(item.getProduct().getProductIcon()).error(R.drawable.icon_kakadef)
                .into(img_icon);
        helper.setText(R.id.tv_apply_people, item.getProduct().getApplyCount() + "");
        helper.addOnClickListener(R.id.btn_apply);

        List<DrainProductBean.ProductBean.MarksBean> marks = item.getProduct().getMarks();
        if(marks.size() > 0){
            DrainProductBean.ProductBean.MarksBean marksBean = marks.get(0);

            String[] split = marksBean.getMarkDesc().split(",");
            LinearLayout linearLayout = helper.getView(R.id.ll_contain);
            linearLayout.removeAllViews();
            for (int i = 0; i < split.length; i++) {
                // TODO 动态添加布局(java方式)
                LayoutInflater inflater = mActivity.getLayoutInflater();
                LinearLayout view = (LinearLayout) inflater
                        .inflate(R.layout.item_lebal, null);
                TextView textView1 = view.findViewById(R.id.tv_target);
                textView1.setText(split[i].trim());
                linearLayout.addView(view);
            }
        }else{
            helper.getView(R.id.ll_contain).setVisibility(View.GONE);
        }
    }
}
